<?php

/**
 * This is the model class for table "exams".
 *
 * The followings are the available columns in table 'exams':
 * @property integer $exams_id
 * @property string $exams_name
 * @property integer $exams_times
 * @property string $exams_memo
 * @property integer $exams_tests_id
 * @property integer $exams_statuts
 * @property integer $exams_creater_id
 * @property string $exams_create_time
 * @property string $exams_edit_tims
 */
class Exams extends CActiveRecord
{
	const UPSTATE = 1;
	const DOWNSTATE = 2;
	
	public static function getStateArr()
	{
		return array(
			self::UPSTATE => '开启',
			self::DOWNSTATE =>'停止',
		);
	}
	public function getStateText($type="") {
		if($type != ""){
			$this->exams_statuts = $type;
		}
		$options = $this->getStateArr();
		return isset($options[$this->exams_statuts])?$options[$this->exams_statuts]:'未定义';
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Exams the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'exams';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('exams_name', 'length', 'max'=>50),
			array('exams_times','numerical'),
			array('exams_statuts','in','range'=>array(1,2)),
			array('exams_name,exams_memo,exams_times,exams_tests_id,exams_statuts','required'),
			array('exams_name,exams_memo,exams_times,exams_tests_id,exams_statuts','safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('exams_id, exams_name, exams_times, exams_memo, exams_tests_id, exams_statuts, exams_creater_id, exams_create_time, exams_edit_tims', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author'=>array(self::BELONGS_TO,'User','exams_creater_id'),
			'tests'=>array(self::BELONGS_TO,'Tests','exams_tests_id'),
			'checks'=>array(self::HAS_MANY,'Checks','checks_tests_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'exams_id' => '考试管理',
			'exams_name' => '考试名称',
			'exams_times' => '考试时长(分)',
			'exams_memo' => '考试说明',
			'exams_tests_id' => '试卷',
			'exams_statuts' => '考试状态',
			'exams_creater_id' => '考试创建人',
			'exams_create_time' => '创建时间',
			'exams_edit_tims' => '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('exams_id',$this->exams_id);

		$criteria->compare('exams_name',$this->exams_name,true);

		$criteria->compare('exams_times',$this->exams_times);

		$criteria->compare('exams_memo',$this->exams_memo,true);

		$criteria->compare('exams_tests_id',$this->exams_tests_id);

		$criteria->compare('exams_statuts',$this->exams_statuts);

		$criteria->compare('exams_creater_id',$this->exams_creater_id);

		$criteria->compare('exams_create_time',$this->exams_create_time,true);

		$criteria->compare('exams_edit_tims',$this->exams_edit_tims,true);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function newSearch($newData = array())
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('exams_id',$this->exams_id);

		$criteria->compare('exams_name',$this->exams_name,true);

		$criteria->compare('exams_times',$this->exams_times);

		$criteria->compare('exams_memo',$this->exams_memo,true);

		$criteria->compare('exams_tests_id',$this->exams_tests_id);

		$criteria->compare('exams_statuts',$this->exams_statuts);

		$criteria->compare('exams_creater_id',$this->exams_creater_id);

		$criteria->compare('exams_create_time',$this->exams_create_time,true);

		$criteria->compare('exams_edit_tims',$this->exams_edit_tims,true);
		
		$criteria->compare('checks.checks_status',$newData["checks_status"],true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function beforeSave() {
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->exams_creater_id = Yii::app()->user->id;
				$this->exams_create_time = $this->exams_edit_tims = date("Y-m-d H:i:s");
			}else{
				$this->exams_edit_tims = date("Y-m-d H:i:s");
			}
			return true;
		}else{
			return false;
		};
	}
	public function filterData()
	{
		if(Yii::app()->user->getState('roles') != 0)
		{
			$this->exams_creater_id = Yii::app()->user->id;
		}
	}
	
}